package com.izhangqian.signproject.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.izhangqian.signproject.plugin.PluginApk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class LoadUtils {

    //把asset目录下的插件apk文件下载到指定文件缓存中
    public static String copyAssetAndWrite(Context context, String fileName) {
        try {
            File cacheDir = new File(context.getFilesDir() + File.separator + "plugins");
            if(!cacheDir.exists()) {
                cacheDir.mkdirs();
            }
            File outFile = new File(cacheDir, fileName);
            if(!outFile.exists()) {
                boolean res = outFile.createNewFile();
                if(res){
                    InputStream is = context.getAssets().open(fileName);
                    FileOutputStream fos = new FileOutputStream(outFile);
                    byte[] buffer = new byte[is.available()];
                    int byteCount;
                    while ((byteCount = is.read(buffer)) != -1){
                        fos.write(buffer, 0, byteCount);
                    }
                    fos.flush();;
                    is.close();;
                    fos.close();
                    Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
                    return outFile.getAbsolutePath();
                }
            }else{
                Toast.makeText(context, "文件已存在", Toast.LENGTH_SHORT).show();
                return outFile.getAbsolutePath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String copyAssetAndWrite(Context context, File srcFile) {
        try {
            String path = context.getFilesDir().getAbsolutePath() + File.separator + "plugins";
            File cacheDir = new File(path);
            if(!cacheDir.exists()){
                cacheDir.mkdirs();
            }
            File outFile = new File(cacheDir, srcFile.getName().replace("mp4", "apk"));
            if(!outFile.exists()){
                boolean res = outFile.createNewFile();
                if(res){
                    InputStream is = new FileInputStream(srcFile);
                    FileOutputStream fos = new FileOutputStream(outFile);
                    byte[] buffer = new byte[is.available()];
                    int byteCount;
                    while ((byteCount = is.read(buffer)) != -1){
                        fos.write(buffer, 0, byteCount);
                    }
                    fos.flush();;
                    is.close();;
                    fos.close();
                    Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
                    return outFile.getAbsolutePath();
                }
            }else{
                outFile.delete();
                boolean res = outFile.createNewFile();
                if(res){
                    InputStream is = new FileInputStream(srcFile);
                    FileOutputStream fos = new FileOutputStream(outFile);
                    byte[] buffer = new byte[is.available()];
                    int byteCount;
                    while ((byteCount = is.read(buffer)) != -1){
                        fos.write(buffer, 0, byteCount);
                    }
                    fos.flush();;
                    is.close();;
                    fos.close();
                    Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
                    return outFile.getAbsolutePath();
                }
                return outFile.getAbsolutePath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static List<String> getFilesFromLocal(Context context, String folderPath) {
        if (TextUtils.isEmpty(folderPath)) {
            return null;
        }
        File file = new File(folderPath);
        if (!file.exists()) {
            file.mkdir();
            return null;
        }
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            return null;
        }
        List<String> apkList = new ArrayList<>();
        for (File file1 : files) {
            if (file1.getName().endsWith("apk") || file1.getName().endsWith("mp4")) {
                String out = copyAssetAndWrite(context, file1);
                apkList.add(out);
            }
        }
        return apkList;
    }

    public static List<String> getApksFromData(Context context) {
        if (context == null) {
            return null;
        }
        try {
            String pluginDir = context.getFilesDir().getAbsoluteFile() + File.separator + "plugins";
            File cacheDir = new File(pluginDir);
            if(!cacheDir.exists()){
                cacheDir.mkdirs();
            }
            File[] files = cacheDir.listFiles();
            if (files == null || files.length == 0) {
                return null;
            }
            List<String> paths = new ArrayList<>();
            for (File file : files) {
                paths.add(file.getAbsolutePath());
            }
            return paths;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
